/*
 * Copyright (c) 2011 Jean-Marc Perronne
 * Ensisa, 12 rue des Freres Lumieres, F-68093 Mulhouse cedex
 * All rights reserved.
 *
 * The copyright of this software is owned by Werner Randelshofer. 
 * You may not use, copy or modify this software, except in  
 * accordance with the license agreement you entered into with  
 * Jean-Marc Perronne. For details see accompanying license terms. 
 */

package fr.uha.ensisa.jmp.drone.com;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

import fr.uha.ensisa.jmp.drone.app.DroneConstants;


public abstract class ComInterface
{
	protected static InetAddress localAddress = null;
	protected static InetAddress droneAddress = null;

	
	private static void initializeLocalAddress(String networkInterface) throws SocketException, BadInterfaceException
	{
		NetworkInterface nif = NetworkInterface.getByName(networkInterface);
		if (nif==null) throw new BadInterfaceException(networkInterface);
		Enumeration<InetAddress> nifAddresses = nif.getInetAddresses();
		localAddress = nifAddresses.nextElement();		
	}
	
	private static void initializeDroneAddress(String droneIp) throws UnknownHostException 
	{		
		droneAddress = InetAddress.getByName(droneIp);
	}
	
	ComInterface() throws SocketException, UnknownHostException, BadInterfaceException
	{
		if (localAddress==null) 
		{
			initializeLocalAddress(DroneConstants.NETWORK_INTERFACE);
		}
		if (droneAddress==null)
		{
			initializeDroneAddress(DroneConstants.WIFI_ARDRONE_IP);
		}
	}
}
